package com.example.cpl.gjc;



import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class user_manage extends AppCompatActivity {
    private Button change_passwd,teac_logout,teac_sign;
    private TextView teac_account,teac_password;
    private int t=0;
    private TextView user_login_name,user_online_num;
    List<String> list;
    ListView listView;
    ArrayAdapter<String> adapter;
    static Handler mHandler;
    Thread t1;
    private static final String TAG = "TAG";
    static ExecutorService mThreadPool;
    public void initWidght(){
        listView=(ListView)findViewById(R.id.user_list);
        change_passwd=(Button)findViewById(R.id.user_change_passwd);
        teac_logout=(Button)findViewById(R.id.user_logout);
        teac_sign=(Button)findViewById(R.id.user_sign);
        teac_account=(TextView)findViewById(R.id.user_account);
        teac_password=(TextView)findViewById(R.id.user_password);
        user_login_name=(TextView)findViewById(R.id.user_login_name);
        user_online_num=(TextView)findViewById(R.id.user_online_num);
        user_login_name.setText(teach_center.who);
        user_online_num.setText(""+device_manage.dev_num+"");

    }
    String filename1 = "teacher";

    public String read_SP_Str(String s,String s1) {     //读取数据库的 字符串类型的值
        final SharedPreferences someData = getSharedPreferences(filename1, 0);
        return  someData.getString(""+s+"",""+s1+"");
    }
    public int read_SP_Int(String s,int s1) {       //读取数据库的 数值类型的值
        final SharedPreferences someData = getSharedPreferences(filename1, 0);
        return  someData.getInt(""+s+"",s1);
    }


    public void  write_SP_Str(String s,String s1) { //写入数据库的 字符串类型的值
        final SharedPreferences someData = getSharedPreferences(filename1, 0);
        SharedPreferences.Editor editor = someData.edit();      //让文件 可以编辑
        editor.putString(""+s+"",""+s1+"");
        editor.commit();// 提交修改
    }
    public void  write_SP_Int(String s,int s1) {            //写入数据库的 数值类型的值
        final SharedPreferences someData = getSharedPreferences(filename1, 0);
        SharedPreferences.Editor editor = someData.edit();      //让文件 可以编辑
        editor.putInt(""+s+"",s1);
        editor.commit();// 提交修改
    }
    public void clear_SP()
    {
        final SharedPreferences someData = getSharedPreferences(filename1, 0);
        SharedPreferences.Editor editor = someData.edit();
        editor.clear();
        editor.commit();

    }
    private void setSP() {

        t= read_SP_Int("value", 0);
        Toast.makeText(getApplicationContext(), "注册数："+t, Toast.LENGTH_SHORT)
                .show();//阴影提示框
        // Log.d("存储数量：",a);
//        for (int i =0; i < t ; i++) {              //循环写入历史数据
//            String account = read_SP_Str("体重" + "" + i + "", "");
//            String account = read_SP_Str("体重" + "" + i + "", "");
//            String password = read_SP_Str("身高" + "" + i + "", "");
//
//
//
//       //     tv_data.setText(tv_data.getText() + "记录时间 "+""+time_data+""+"\n"+sg_data + "    " + tz_data +"\n"+bmi_data+"\n\n");
//        }
    }

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_manage);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        initWidght();
        list = new ArrayList<String>();
        setSP();//循环写入历史数据
        change_passwd.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("PrivateResource")
            @Override
            public void onClick(View view) {

                String account=null;                     //得到记录的数据
                account=teac_account.getText().toString();
                String password=null;
                password=teac_password.getText().toString();

                if(!account.equals("")&&!password.equals("")){

                    write_SP_Str("account" + "" + t + "",  account);
                    write_SP_Str("password" + "" + t + "",  password);

                    t++;
                    write_SP_Int("value", t);
                    Toast.makeText(getApplicationContext(), "修改密码成功", Toast.LENGTH_SHORT)
                            .show();//阴影提示框

                }else{
                    Toast.makeText(getApplicationContext(), "输入错误", Toast.LENGTH_SHORT)
                            .show();//阴影提示框
                }


            }
        });

        teac_sign.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("PrivateResource")
            @Override
            public void onClick(View view) {

                String account=null;                     //得到记录的数据
                account=teac_account.getText().toString();
                String password=null;
                password=teac_password.getText().toString();

                if(!account.equals("")&&!password.equals("")){

                    write_SP_Str("account" + "" + t + "",  account);
                    write_SP_Str("password" + "" + t + "",  password);

                    t++;
                    write_SP_Int("value", t);
                    Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT)
                            .show();//阴影提示框

                }else{
                    Toast.makeText(getApplicationContext(), "输入错误", Toast.LENGTH_SHORT)
                            .show();//阴影提示框
                }


            }
        });


        teac_logout.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("PrivateResource")
            @Override
            public void onClick(View view) {

                String account=null;                     //得到记录的数据
                account=teac_account.getText().toString();

                if(!account.equals("")){

                    write_SP_Str("account" + "" + t + "",  account);
                    write_SP_Str("password" + "" + t + "",  "~!~@#$%");
                    Toast.makeText(getApplicationContext(), "已注销", Toast.LENGTH_SHORT)
                            .show();//阴影提示框

                }else{
                    Toast.makeText(getApplicationContext(), "输入账号错误", Toast.LENGTH_SHORT)
                            .show();//阴影提示框
                }

                teac_account.setText("");
                teac_password.setText("");

                Toast.makeText(getApplicationContext(), "已注销账号！", Toast.LENGTH_SHORT)
                        .show();//阴影提示框

            }
        });
        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
        mThreadPool = Executors.newCachedThreadPool();   // 初始化线程池

        mHandler = new Handler()
        {                    // 实例化主线程,用于更新接收过来的消息
            @Override
            public void handleMessage(android.os.Message msg)
            {

                Log.d(TAG, "recv obj: "+msg.obj);
                Log.d(TAG, "recv what: "+msg.what);
                switch (msg.what){
                    case 0x01:

                        list.add(msg.obj.toString());
                        msg.what=0;
                        break;

                }
                listView.setAdapter(adapter);
            }
        };
        t1=new Thread()
        {

            public void run(){
                t= read_SP_Int("value", 0);
                for(int p=0;p<t;p++) {
                    String account=null;                     //得到记录的数据
                    account=read_SP_Str("account" + "" + p + "","");
                    String password=null;
                    password=read_SP_Str("password" + "" + p + "","");

                    try {
                            Message msg=new Message();
                            msg.what = 0x01;
                            msg.obj = "用户：" + account+"，密码："+password;
                            mHandler.sendMessage(msg);

                    }catch (Exception e){
                        System.out.println("catch err");
                    }
                }


            }
        };
        if(teach_center.who.equals("8888")) {
            t1.start();
        }



    }
}
